package servlet.article;

import entity.Article;
import util.*;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.*;

import static util.BaseDao.*;

public class CourselServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Connection connection = null;
        PreparedStatement preparedStatement = null;
        List<Article> list = new ArrayList();
        Number total = 0;
        try {
            // 获取参数
            Article params = (Article) GetParamToClass.setObjectFiled(req, Article.class);
            Map<String, Object> pIndex = new LinkedHashMap<>(); //sql参数
            String page1 = req.getParameter("page");
            Integer page = 0;
            Integer pageSize = 10;
            if (!Empty.isEmpty(page1)) {
                page = Integer.valueOf(page1)-1;
            }
            if (!Empty.isEmpty(req.getParameter("pageSize"))) {
                pageSize = Integer.valueOf(req.getParameter("pageSize"));
            }
            String order = req.getParameter("order"); //DESC ASC
            if ("".equals(order) || null == order) {
                order = "DESC";
            }
            connection = DButils.getConnection();
            String tableName = getTableName(Article.class);
            //把实体类属性名转换成数据库中带下划线的字段名 例： UserName->user_name
            List<String> fields = getFields(Article.class);
            StringBuilder sb = new StringBuilder("SELECT  ");
            sb.append(String.join(",", fields));
            sb.append(" FROM " + tableName);
            sb.append(" WHERE carousel = 1 ");
            sb.append(" ORDER BY create_time " + order + " LIMIT ?,?");
            pIndex.put("page", page);
            pIndex.put("pageSize", pageSize);
            String sql = sb.toString();
            preparedStatement = connection.prepareStatement(sql);
            Set<String> strings = pIndex.keySet(); // sql参数 keyset
            Iterator<String> iterator = strings.iterator(); // keyset 迭代器，遍历参数名和值，
            //获取map keyset 设置sql参数
            for (int j = 0; j < strings.size(); j++) {
                Object o = "";
                if (iterator.hasNext()) {
                    o = iterator.next();
                    preparedStatement.setObject(j+1 , pIndex.get(o));
                }

            }
            //获取PrepareStatement里面编译好的SQL语句
            ResultSet resultSet = preparedStatement.executeQuery(); // 查询结果
            System.out.println(getSql(preparedStatement));
            while (resultSet.next()) { // 判断是否有数据
                Article article = null;
                article = (Article) setField(fields,Article.class,resultSet);
                // 把查询到的一行加入list
                list.add(article);
            }
            ResposeUtil.write(R.data(R.CODE_SUCCESS,list), resp);
        } catch (Exception e) {
            e.printStackTrace();
            ResposeUtil.write(R.data(R.CODE_SYS_ERR, R.MSG_SYS_ERR), resp);
        } finally {
            DButils.close(connection, preparedStatement);
        }
    }
}
